﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using WebUI.Controls;

namespace WebUI
{
    public partial class Supplier : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (!IsPostBack)
            {
                Reload();
            }

            txtSearch.Focus();

            if (SessionUser.BranchID != Branches.CurrentBranchID ||  !SessionUser.Admin)
            {
                btnAddSupplier.Enabled = false;
                btnEditSupplier.Enabled = false;
                btnDeleteSupplier.Enabled = false;

                btnAddSupplier.OnClientClick = "";
                btnEditSupplier.OnClientClick = "";
                btnDeleteSupplier.OnClientClick = "";
            }
        }

        private void Reload()
        {
            Suppliers supp = Suppliers.GetAllActiveSuppliers();
            lstSupplier.SupplierList = supp;

            RefreshSupplierList();
        }

        private void RefreshSupplierList()
        {
            gridFilterPaging.Reset();

            string oldFilter = lstSupplier.Filter;

            lstSupplier.Filter = Suppliers.ColumnNames.Deleted + "=0";

            gridFilterPaging.DefaultFilter = lstSupplier.Filter;

            lstSupplier.Refresh();
        }

        public string GetSupplierEditPageLink()
        {
            return "EditPages/EditSupplier.aspx?key=" + lstSupplier.SourceObjectSessionID;
        }

        protected void btnAddSupplier_OnClick(Object sender, EventArgs e)
        {
            lstSupplier.Refresh();
        }

        protected void btnRemoveSupplier_OnClick(object sender, EventArgs e)
        {
            lstSupplier.DeleteCheckedItems();
            RefreshSupplierList();
        }

        protected void btnPrintSupplier_OnClick(object sender, EventArgs e)
        {

            SupplierListCtrl printedList = (SupplierListCtrl)LoadControl(@"~/Controls/POSControls/SupplierListCtrl.ascx");
            printedList.AllowPaging = false;
            printedList.AllowSelecting = false;
            printedList.AllowSorting = false;
            printedList.SourceObject = lstSupplier.SupplierList;
            printedList.Width = new Unit(100, UnitType.Percentage);
            printedList.GridLineStyle = GridLines.Both;
            printedList.GridBorder = BorderStyle.Solid;
            printedList.GridBorderWidth = new Unit(1, UnitType.Pixel);
            printedList.CssClass = "";
            printedList.Refresh();

            Session[PrintKey] = printedList;
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showPrintDlg('{0}');", GetPrintPage()), true);

            lstSupplier.Refresh();
        }

        protected void btnExcel_OnClick(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(lstSupplier.SupplierList, "Supplier.xls",
                new String[] { "SupplierName", "ABN", "Address", "Phone", "AccountNo", "Fax", "Email" },
                new String[] { "Customer", "ABN", "Address", "Phone", "AccountNo", "Fax", "Email" },
                "Supplier");
        }

        protected void btnSearchSupplier_OnClick(object sender, EventArgs e)
        {
            gridFilterPaging.Reset();
            if (gridFilterPaging.DefaultFilter != "")
                lstSupplier.Filter = gridFilterPaging.DefaultFilter + " and (" + Suppliers.ColumnNames.SupplierName + " LIKE '%" + txtSearch.Text + "%')";
            else
                lstSupplier.Filter = Suppliers.ColumnNames.SupplierName + " LIKE '%" + txtSearch.Text + "%'";
            lstSupplier.Refresh();
        }

        protected void btnReload_OnClick(object sender, EventArgs e)
        {
            Reload();
        }

        protected void gridFilterPaging_OnNeedListControl(object sender, EventArgs e)
        {
            gridFilterPaging.ListControl = lstSupplier;
        }

        public string PrintKey
        {
            get
            {
                if (ViewState["PrintKey"] == null)
                    ViewState["PrintKey"] = Guid.NewGuid().ToString();
                return ViewState["PrintKey"].ToString();
            }
        }

        public string GetPrintPage()
        {
            return "EditPages/PrintHandler.aspx?key=" + PrintKey;
        }
    }
}
